package org.stragma.symbian;

import java.util.*;

import aterm.*;
import aterm.pure.PureFactory;
import org.stragma.symbian.aterms.*;

/**
 * @author shirren
 * This class implements the statistics command which provides results on a particular tree
 */
public class StatisticsCommand implements ICommand {

	private String[] args;
	
	/**
	 * @param args2
	 * Custom constructor which accepts a list of arguments
	 */
	public StatisticsCommand(String[] args) {
		if (args.length != 2) {
			throw new IllegalArgumentException("The statistics command only takes one argument.");
		}
		this.args = args;
	}
	
	/* (non-Javadoc)
	 * @see org.stragma.symbian.ICommand#execute(java.util.Iterator)
	 */
	@Override
	public void execute() {
		
		// We only care about the all the arguments after the first one
		Iterator<String> arguments = Arrays.asList(this.args).iterator();
		arguments.next(); // Skip the first argument as it is the command line switch
		
		while (arguments.hasNext()) {
			String fileName = arguments.next();
				
			try {
				ATermFactory termFactory = new PureFactory();
				ATerm rootTerm = termFactory.readFromFile(fileName);
				
				IATermStat stat = new ATermStat();
				System.out.println("----------------\n");
				System.out.println("Number Of Nodes: " + stat.getTotalNodes(rootTerm));
				System.out.println("Number Of Terms: " + stat.getSubTerms(rootTerm));
				System.out.println("Number Of Unique Terms: " + stat.getUniqueSubTerms(rootTerm));
				
				IATermTreeDepth depthStat = new ATermTreeDepth();
				System.out.println("Depth Of Tree: " + depthStat.getMaximumDepth(rootTerm));
				
				IATermStrategoStat ruleStat = new ATermStrategoRuleCheck();
				System.out.println("Number Of Rules: " + ruleStat.getNumberOfSpecificTerms(rootTerm));
				
				IATermStrategoStat stratStat = new ATermStrategoStrategyCheck();
				System.out.println("Number Of Strategies: " + stratStat.getNumberOfSpecificTerms(rootTerm));
				System.out.println("----------------\n");
				
			} catch (Exception ex) {
				System.out.println(ex.getMessage());
			}
		}
	}
}
